#include "fxcc/core/graph/opengl3/ImGuiGlfwApp.h"
#include "fxcc/core/graph/common/Geometry.h"
#include "fxcc/imgui/Editor.h"
#include "fxcc/core/graph/common/pch.h"

#include "fxcc/core/audio/common/Resources.h"
#include "fxcc/core/audio/openal/Source.h"
#include "fxcc/core/audio/openal/World.h"

#include "fxcc/core/AssertManager.h"

using namespace fxcc::platform;
using namespace fxcc::graph;
using namespace fxcc::graph::opengl3;
using namespace fxcc::imgui::graph::opengl3;
using BaseApp = fxcc::imgui::graph::opengl3::GlfwApp;

#undef main

struct SimpleApp : public BaseApp
{
public:
    fxcc::graph::common::Transform transform;

    std::shared_ptr<fxcc::graph::opengl3::RenderTargetJack> m_RenderTarget;
    std::shared_ptr<fxcc::graph::opengl3::MeshJack> m_CubeMesh;

    glm::vec4 color;
    std::shared_ptr<fxcc::graph::opengl3::Effect3d> m_Effect;

    fxcc::graph::common::Camera camera;

    SimpleApp(const fxcc::platform::common::App::Desc &desc) : BaseApp(desc), color(1.0)
    {
    }
    bool Init() override
    {
        if (!BaseApp::Init())
        {
            return false;
        }

        if (!InitResources())
        {
            return false;
        }

        fxcc::audio::openal::World::Inst();

        auto& rs2 = fxcc::audio::common::Resources::Inst();
        {

            std::string dir = fxcc::AssertManager::Inst().GetFile("wavFiles");
            for (const auto _p : fs::directory_iterator(dir))
            {
                std::string _pathStr = _p.path().string();
                rs2.LoadPath(_pathStr);
            }
        }
        return true;
    };

    void printOpenGLESVersion()
    {
        const char *version = (const char *)glGetString(GL_VERSION);
        std::cout << "OpenGL ES Version: " << version << std::endl;
    };


    bool InitResources()
    {


        camera.SetPosition(glm::vec3(5.0f));
        camera.LookAt(glm::vec3(0.0f));

        return true;
    };

    fxcc::imgui::Editor editor;
    ;

    void OnEdit() override
    {
        auto& audioMap = fxcc::audio::common::Resources::Inst();


        ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
        ImGui::Begin("AudioList");
        for (auto [desc, key] : audioMap.m_Sources.m_DataList) {
         
            editor.EditInst<fxcc::audio::common::Source>("", *key);
        }
        ImGui::End();
        ImGui::Begin("Play");

        
        for (auto [desc, key] : audioMap.m_Sources.m_DataList) {

            ImGui::PushID(key.get());
            ImGui::Text("%s", desc.GetFilePath().c_str());
            ImGui::SameLine();
            if (ImGui::Button("Play"))
            {
                key->Play();
            }
            ImGui::SameLine();

            if (ImGui::Button("Pause"))
            {
                key->Pause();
            }
            ImGui::SameLine();

            if (ImGui::Button("Stop"))
            {
                key->Stop();
            }
            ImGui::PopID();
        }
   
        ImGui::End();
    }
    void OnDestroy() override
    {
      
        BaseApp::OnDestroy();
    }
    void OnFrameRender() override
    {
        glViewport(0, 0, m_Desc.m_Size.x, m_Desc.m_Size.y);

        glClearColor(color.x, color.y, color.z, color.w);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    };

    void OnFramebuffersize(int w, int h) override
    {
        opengl3::GlfwApp::OnFramebuffersize(w, h);
        camera.SetAspect(m_Desc.Aspect());


    }
    void OnUpdate() override
    {
        opengl3::GlfwApp::OnUpdate();

        float ctTime = m_GlfwlTimer.m_CurrentTime;
        camera.FreeMove(m_Input, m_GlfwlTimer.m_DeltaTime);

        float deltaTime = m_GlfwlTimer.m_DeltaTime;


    }
};

int main()
{
    ztclog::pushFunction([](const ztclog::Line &line)
                         {
		if (line.m_Type != ztclog::Line::Type::_line_debug_)
		{
			std::cout << line.m_Text << std::endl;
		} });

    using _App = SimpleApp;
    using _Desc = fxcc::platform::common::App::Desc;

    _Desc desc;
    desc.m_Pos = glm::ivec2(100, 100);
    desc.m_Size = glm::ivec2(900, 600);
    desc.m_ScaleFactor = 1.0f;

    desc.m_Title = "test font";
    std::shared_ptr<_App> _app = std::make_shared<_App>(desc);
    return _app->Run();
};
